﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BartonExtension
{
    public class FileIO
    {
        public static bool isFileCanUse(string file)
        {
            bool fileCanUse = true;
            try
            {
                using(FileStream fs = File.Open(file,FileMode.Open))
                {
                    fs.Close();
                }
            }
            catch(Exception e)
            {
                fileCanUse = false;
            }
            return fileCanUse;
        }
        public static string ValidFileName(string filename)
        {
            string ori_fname = Path.GetFileNameWithoutExtension(filename);
            string directory = filename.Replace(Path.GetFileName(filename), "");
            string ext = Path.GetExtension(filename);
            string fname = ori_fname;
            int tag = 0;
            while(File.Exists(directory + fname + ext))
            {
                fname = ori_fname + "({0})".FormatWith(tag);
                tag++;
            }
            return fname;
        }

        public static long FileLength(string filename)
        {
            try
            {
                FileInfo info = new FileInfo(filename);

                return info.Length;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}
